package com.mc.moring.login.modle

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.google.gson.GsonBuilder
import com.google.gson.ToNumberPolicy
import com.mc.moring.app.LogUtils
import com.mc.moring.base.BaseModle
import com.mc.moring.base.HttpBitmapCallback
import com.mc.moring.base.HttpCallback
import com.mc.moring.core.UUIDCoreData
import com.mc.moring.netconfig.Apis
import com.mc.moring.util.GsonUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.ResponseBody


class LoginModle : BaseModle() {

    private val coroutineScope = CoroutineScope(Dispatchers.Default)

    companion object {
        val instant by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            LoginModle()
        }
    }

    fun getLoginZiLiPageData(country: String, mobile: String,httpCallback: HttpCallback) {

        coroutineScope.launch {
            try {
                var result: String? = ""
                val map = HashMap<String, String>()
                map["country"] = country
                map["mobile"] = mobile
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoginServer.checkZiLiPageMobileRegister(request)
                result = decryptingZiLiPageData(data)
                httpCallback.onSuccess(result?:"")
                LogUtils.logE("e ：$result")
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }


    fun getZiLiPageConfigInfoByAppId(httpCallback: HttpCallback){

        coroutineScope.launch {
            try {
                var result: String? = ""
                val map = HashMap<String, String>()
                map["appId"] = ZiLiPage_App_ID
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoginServer.getZiLiPageConfigInfoByAppId(request)
                result = decryptingZiLiPageData(data)
                httpCallback.onSuccess(result?:"")
                LogUtils.logE("e ：$result")
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun getZiLiPagesecurityLogin(
        account: String,
        password: String,
        context: Context,
        captcha: String?,
        httpCallback: HttpCallback
    ){

        coroutineScope.launch {
            try {
                var result = ""
                val map = HashMap<String, String>()
                map["country"] = "56"
                map["mobile"] = account ?: ""
                map["password"] = password ?: ""
//                map["deviceId"] = getZiLiPageDeviceId(context = context).toString()
                map["deviceId"] = UUIDCoreData.getUUID()
                map["captcha"] = captcha ?: ""
                LogUtils.logE("--getZiLiPagesecurityLogin mapString:${GsonUtils.getGson().toJson(map)}")
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoginServer.getZiLiPagesecurityLogin(request)
                result = decryptingZiLiPageData(data)
                httpCallback.onSuccess(result?:"")
                LogUtils.logE("request success ：${map.toString()}...$result")
            } catch (e: Exception) {
                LogUtils.logE("request error :$e")
            }
        }
    }

    fun getZiLiPageCaptcha(mobile:String, scenes:String, httpCallback: HttpBitmapCallback) {

        coroutineScope.launch {
            try {
                var bitmap: Bitmap? = null
                val map = HashMap<String, String>()
                map["scenes"] = scenes
                map["mobile"] = mobile
                LogUtils.logE("--getZiLiPageCaptcha mapString:${GsonUtils.getGson().toJson(map)}")
                var requestValue = GsonBuilder()
                    .setObjectToNumberStrategy(ToNumberPolicy.LAZILY_PARSED_NUMBER)
                    .create()
                    .toJson(map)

                var request: RequestBody =
                    requestValue.toRequestBody("application/json".toMediaTypeOrNull())
                var data = Apis.apiLoginNewServer.getZiLiPageCaptcha(request)
//                val data = Apis.apiIsHeadServer.getZiLiPageCaptcha(request)
//                var data = Apis.apiLoginServer.getZiLiPageCaptcha(request)
                var responseBody: ResponseBody? = data.body()
                try {
                    var inputstream = responseBody?.byteStream()
                    bitmap = BitmapFactory.decodeStream(inputstream)
                    httpCallback.onSuccess(bitmap)
                } catch (e: java.lang.Exception) {
                    e.printStackTrace()
                }
                LogUtils.logE("e ：$bitmap")
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun initZiLiPageDeviceSeed(
        context: Context,
        httpCallback: HttpCallback
    ){
        var result: String = ""
        coroutineScope.launch {
            try {
                val map = HashMap<String, String>()
                map["deviceId"] = getZiLiPageDeviceID(context)?:""
                val mapString = getZiLiPageJson(map, false)
                LogUtils.logE("--initZiLiPageDeviceSeed mapString:${GsonUtils.getGson().toJson(map)}")
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoginServer.initZiLiPageDeviceSeed(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e :$e")
            }
        }
    }

    fun getZiLiPageCode(context: Context,
                        scenes: String,
                        mobile: String, smsType:String,
                        captcha:String,
                        anonymousToken:String, time: String,
                        httpCallback: HttpCallback) {

        coroutineScope.launch {
            try {
                var result: String = ""
                val map = HashMap<String, String>()
                map["scenes"] = scenes
                map["mobile"] = mobile
                map["country"] = "56"
                map["deviceId"] = getZiLiPageDeviceId(context = context).toString()
                map["time"] = time.toString() + ""
                map["smsType"] = smsType
                map["captcha"] = captcha
                map["anonymousToken"] = anonymousToken
                LogUtils.logE("--getZiLiPageSmsCode mapString:${GsonUtils.getGson().toJson(map)}")
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoginServer.getZiLiPageCode(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                httpCallback.onSuccess(result)
                LogUtils.logE("e ：1$result")
            } catch (e: Exception) {
                LogUtils.logE("e ：2$e")
            }
        }
    }

    fun verifyZiLiPageSCodeForOTP(
        mobile: String,
        sCode: String,
        context: Context,
        httpCallback: HttpCallback
    ){
        coroutineScope.launch {
            try {
                var result: String = ""
                val map = HashMap<String, String>()
                map["country"] = "56"
                map["mobile"] = mobile
                map["smsCode"] = sCode
                map["deviceId"] = getZiLiPageDeviceId(context = context).toString()
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoginServer.verifyZiLiPageCodeForOTP(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e :$e")
            }
        }
    }

    fun loginByS(
        mobile: String,
        sCode: String,
        context: Context,
        httpCallback: HttpCallback
    ){
        coroutineScope.launch {
            try {
                var result: String = ""
                val map = HashMap<String, String>()
                map["country"] = "56"
                map["mobile"] = mobile
                map["smsCode"] = sCode
                map["deviceId"] = getZiLiPageDeviceId(context = context).toString()
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoginServer.loginByCode(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e :$e")
            }
        }
    }

    fun accountZiLiPageRegister(map: MutableMap<String,String>,httpCallback: HttpCallback){

        coroutineScope.launch {
            try {
                var result: String = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoginServer.accountZiLiPageRegister(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e :$e")
            }
        }
    }

    fun resetZiLiPagePassword(country: String,
                              mobile: String,
                              smsCode: String,
                              password: String,
                              rePassword: String,
                              httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result: String? = ""
                val map = HashMap<String, String>()
                map["country"] = country
                map["mobile"] = mobile
                map["smsCode"] = smsCode
                map["password"] = password
                map["rePassword"] = rePassword
                val mapString = getZiLiPageJson(map, false)
                LogUtils.logE("--mapString:${GsonUtils.getGson().toJson(map)}")
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoginServer.resetLoginPassword(request)
                result = decryptingZiLiPageData(data)
                httpCallback.onSuccess(result?:"")
                LogUtils.logE("e resetpwd：$result")
            } catch (e: Exception) {
                LogUtils.logE("e -->resetpwd：$e")
            }
        }

    }


}